namespace core {
    export class ObjectPool {
        private _objs: Array<any>;

        public constructor() {
            this._objs = new Array<any>();
        }

        public pushObj(obj: any): void {
            this._objs.push(obj);
        }

        public popObj(): any {
            if (this._objs.length > 0) {
                return this._objs.pop();
            } else {
                return null;
            }
        }

        public clear(): void {
            while (this._objs.length > 0) {
                this._objs.pop();
            }
        }

        private static _content: any = {};
        public static pop(refKey: string, ...args: any[]): any {
            if (!ObjectPool._content[refKey]) {
                ObjectPool._content[refKey] = new Array<any>();
            }
            var list: Array<any> = ObjectPool._content[refKey];

            if (list.length) {
                return list.pop();
            } else {
                var classZ: any = egret.getDefinitionByName(refKey);
                var argsLen: number = args.length;
                var obj: any;
                if (argsLen == 0) {
                    obj = new classZ();
                } else if (argsLen == 1) {
                    obj = new classZ(args[0]);
                } else if (argsLen == 2) {
                    obj = new classZ(args[0], args[1]);
                } else if (argsLen == 3) {
                    obj = new classZ(args[0], args[1], args[2]);
                } else if (argsLen == 4) {
                    obj = new classZ(args[0], args[1], args[2], args[3]);
                } else if (argsLen == 5) {
                    obj = new classZ(args[0], args[1], args[2], args[3], args[4]);
                }
                obj.ObjectPoolKey = refKey;
                return obj;
            }
        }

        public static push(obj: any): boolean {
            if (obj == null) {
                return false;
            }

            var refKey: any = obj.ObjectPoolKey;
            if (!ObjectPool._content[refKey]) {
                return false;
            }

            ObjectPool._content[refKey].push(obj);
            return true;
        }

        public static clear(): void {
            ObjectPool._content = {};
        }
    }
}